<!--
 * @由于个人水平有限, 难免有些错误, 还请指点:  
 * @Author: cpu_code
 * @Date: 2020-10-17 23:05:03
 * @LastEditTime: 2020-10-17 23:32:24
 * @FilePath: \web\javascript\js_logic\js_logic.html
 * @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
 * @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
 * @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
 * @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>逻辑运算符</title>

        <script>
            /*
            * 逻辑运算符:
            *  &&: 与（短路）
            *  ||: 或 (短路)
            *  !：非
            *      * 其他类型转boolean：
            *          1. number：0或NaN为假，其他为真
            *          2. string：除了空字符串("")，其他都是true
            *          3. null&undefined:都是false
            *          4. 对象：所有对象都为true
            */

            var flag = true;
            document.write("flag = " + flag + "<br>");
            document.write("!flag = " + !flag + "<br>");

            document.write("<hr>");

            //数字
            var num = 3;
            var num2 = 33;
            var num3 = NaN;
            document.write("!!num = " + !!num + "<br>");
            document.write("!!num2 = " + !!num2 + "<br>");
            document.write("!!num3 = " + !!num3 + "<br>");

            document.write("<hr>");

            //string
            var str1 = "abc";
            var str2 = "";
            document.write("!!str1 = " + !!str1 + "<br>");
            document.write("!!str2 = " + !!str2 + "<br>");

            document.write("<hr>");

            // null & undefined
            var obj = null;
            var obj2;
            document.write("!!obj = " + !!obj + "<br>");
            document.write("!!obj2 = " + !!obj2 + "<br>");

            document.write("<hr>");
            
            // null & undefined
            var date = new Date();
            document.write("!!date = " + !!date + "<br>");

            document.write("<hr>");

            obj = "111";
            //防止空指针异常
            if(obj != null && obj.length > 0) {
                alert(111);
            }

            //js中可以这样定义，简化书写
            ////防止空指针异常
            if(obj){
                alert(222);
            }
        </script>
    </head>

    <body>

    </body>
</html>